package org.example;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.IOException;

import static java.lang.String.format;

public class StudentManage {
    static String fraction = "";
    static String subjectdj ="";
    static double fraction2;
    static float numberadd = 1;
    static float numberbdd = 1;
    private static Scanner scanner = new Scanner(System.in);
    private static Map<String, student> studentIco = new HashMap<>();


    // 获取菜单
    public static String getMenu() {
        System.out.println("<<<<<<<<<<<<<<<学生学分系统>>>>>>>>>>>>>>>");
        System.out.println("               1、添加学生");
        System.out.println("               2、修改学生");
        System.out.println("               3、删除学生");
        System.out.println("               4、查询学生");
        System.out.println("               5、退出系统");
        System.out.println("<<<<<<<<<<<<<<<学生学分系统>>>>>>>>>>>>>>>");
        System.out.print("请选择功能：");
        String menuSelect = StudentManage.scanner.next();
        return menuSelect;
    }

    // 添加学生
    public static boolean addStudent() {
        System.out.println("请输入学生的学号：");
        String number = StudentManage.scanner.next();
        System.out.println("请输入学生的姓名：");
        String name = StudentManage.scanner.next();
        System.out.println("请输入学生的选课：");
        String subject = StudentManage.scanner.next();
        System.out.println("请输入学生的成绩：");
        String effort = StudentManage.scanner.next();
        float effort1 = Float.parseFloat(effort);
        fraction = String.valueOf(effort1 / 10 - 5);
        if (effort1 >= 95) {
            effort = "A+";
        } else if (effort1 < 95 && effort1 >= 90) {
            effort = "A";
        } else if (effort1 < 90 && effort1 >= 85) {
            effort = "B+";
        } else if (effort1 < 85 && effort1 >= 80) {
            effort = "B";
        } else if (effort1 < 80 && effort1 >= 75) {
            effort = "C";
        } else if (effort1 < 75 && effort1 >= 70) {
            effort = "C-";
        } else if (effort1 < 70 && effort1 >= 60) {
            effort = "D";
        } else if (effort1 < 60) {
            effort = "F";
            fraction = "0.0";
        }
        fraction2 = Float.parseFloat(fraction);
        subjectdj = subject;
        student student = new student();
        student.number = number;
        student.name = name;
        student.subject1 = subjectdj;
        student.effort = effort;
        student.fraction = fraction;
        StudentManage.studentIco.put(student.number, student);
        return true;
    }

    // 修改学生
    public static boolean editStudent() {
        System.out.print("请输入学生学号：");
        String number = scanner.next();
        student student = studentIco.get(number);
        if (student == null) {
            System.out.println("学生不存在！");
            return false;
        }
        System.out.println("修改姓名：");
        String name = StudentManage.scanner.next();
        System.out.println("修改选课：");
        String subject2 = StudentManage.scanner.next();
        System.out.println("修改成绩：");
        String effort = StudentManage.scanner.next();
        float effort1 = Float.parseFloat(effort);
        String fraction1 = String.valueOf(effort1 / 10 - 5);
        student.name = name;
        float fraction11 = Float.parseFloat(fraction1);
        if(numberbdd==1){
            subjectdj = student.subjectys +" "+ subjectdj + " " + subject2;
            fraction2 = fraction2 + student.fractionys;
            numberbdd--;
        }
        else{
            subjectdj = subjectdj + " " + subject2;
        }
        student.subject1 = subjectdj;
        student.TURE="true";
        fraction2 = fraction2 + fraction11;
        numberadd++;
        float average = (float) (fraction2 / numberadd);
        String average1= String.format("%.2f", average);
        average=Float.valueOf(average1);

        if (average >= 4.0) {
            effort = "A+";
        } else if (average < 4.0 && average >= 3.5) {
            effort = "A";
        } else if (average < 3.5 && average >= 3.0) {
            effort = "B+";
        } else if (average < 3.0 && average >= 2.5) {
            effort = "B";
        } else if (average < 2.5 && average >= 2.0) {
            effort = "C";
        } else if (average < 2.0 && average >= 1.5) {
            effort = "C-";
        } else if (average < 1.5 && average >= 1.0) {
            effort = "D";
        } else {
            effort = "F";
        }
        student.fraction = String.valueOf(average);
        student.effort = effort;
        StudentManage.studentIco.put(student.number, student);
        return true;
    }

    // 删除学生
    public static boolean delStudent() {
        System.out.println("请输入删除学生的学号：");
        String number = StudentManage.scanner.next();
        StudentManage.studentIco.remove(number);
        return true;
    }

    // 查询学生
    public static boolean queryStudent() {
        System.out.print("请输入学生学号：");
        String number = StudentManage.scanner.next();
        student student = StudentManage.studentIco.get(number);
        if (student == null) {
            System.out.println("学生不存在！");
            return false;
        }
        float fraction33=0;
        float dao=0;
        if(student.TURE!="true"){
            fraction33 = (float) (fraction2 + student.fractionys);
            dao= (float) fraction2;
            fraction2=fraction33;
        }

        System.out.println("学生学号：" + student.number);
        System.out.println("学生姓名：" + student.name);
        System.out.println("学生成绩：" + student.effort);
        if(student.TURE!="true"){
            System.out.println("学生选课：" + student.subject1);
            System.out.println("学生学分：" + fraction33);
            fraction2=dao;
        }
        else{
            System.out.println("学生选课：" + student.subject1);
            System.out.println("学生学分：" + student.fraction);

        }

        return true;
    }

    // 保存学生信息到文件
    public static boolean saveStudentsToFile(String filePath) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            for (Map.Entry<String, student> entry : studentIco.entrySet()) {
                student student = entry.getValue();
                String studentInfo = format(
                        "%s,%s,%s,%s,%s%n",
                        student.number, student.name, student.subject1, student.effort,student.fraction
                );
                writer.write(studentInfo);
            }
            System.out.println("学生信息已保存到文件：" + filePath);
            return true;
        } catch (IOException e) {
            System.out.println("保存文件时发生错误：" + e.getMessage());
            return false;
        }
    }

    // 从文件读取学生信息
    // 从文件读取学生信息
    public static boolean readFile(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(",");
                if (data.length == 5) {
                    String number = data[0].trim();
                    String name = data[1].trim();
                    String subject = data[2].trim();
                    String effort = data[3].trim();
                    String fraction = data[4].trim();

                    student stu = new student();
                    stu.number = number;
                    stu.name = name;
                    stu.subject1 = subject;
                    stu.subjectys =subject;
                    stu.effort = effort;
                    stu.fraction=fraction;
                    stu.fractionys = Float.valueOf(fraction);



                    studentIco.put(number, stu);
                }
            }
            System.out.println("文件内容已读取到内存。");
            return true;
        } catch (IOException e) {
            System.out.println("读取文件时发生错误：" + e.getMessage());
            return false;
        }
    }

    // 将字母成绩转换为数值学分

}